﻿/* Write a program that concatenates two text files into another text file. */

using System;
using System.IO;
using System.Text;

namespace _2.ConcatenateTextFiles
{
    class ConcatenateTextFiles
    {
        static void Main()
        {
            Console.Write("Path to first file (along with the filename): ");
            string path_to_file = @Console.ReadLine();
            
            Console.Write("Path to second file (along with the filename): ");
            string path_to_file2 = @Console.ReadLine();

            /* read files */
            StreamReader SR = new StreamReader(path_to_file);
            string contents = null;

            try
            {
                using (SR)
                {
                    contents = SR.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }
            
            SR.Dispose();


            SR = new StreamReader(path_to_file2);
            string contents_2 = null;

            try
            {
                using (SR)
                {
                    contents_2 = SR.ReadToEnd();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
            }

            /* concatenate */
            StringBuilder sb = new StringBuilder();
            sb.Append(contents);
            sb.Append(contents_2);
            string contents_all = sb.ToString();
            
            /* Write to another file */
            Console.Write("Path to output file (along with the filename): ");
            string outputFilename = @Console.ReadLine();

            using (FileStream fs = new FileStream(outputFilename, FileMode.Create))
            {
                using (StreamWriter SW = new StreamWriter(fs))
                {
                    SW.Write(contents_all);
                }
            }
            Console.WriteLine("Data successfully written.");
        }
    }
}
